<!--
@fucntion: 教师教学工作简历信息
-->
<template>
<div class="module-basic-teachingresume">
  <el-row style="height:100%;">
    <el-col style="height:100%;">
      <table-panel :name="'教师教学工作简历信息'" :columns="teachingResumeColumns" :tableData="teachingResumeData" :showRowNumber="false" :multiSelection="true"
      :usePagination="false"
      :canCreate="authorities.create" :canEdit="authorities.edit" :canDelete="authorities.delete"
      :currentPage="teachingResumeCurrentPage" :totalNum="teachingResumeTotalNum" @page-change="onPageChange('teachingResume',$event)" :pageSize="pageSize"
      @table-searching="onSearching('teachingResume',$event)"
      @table-create="onCreate('teachingResume',$event)" @table-edit="onEdit('teachingResume', $event)"
      @table-delete="onDelete('teachingResume',$event)" @table-export="onExport('teachingResume',$event)"
      @selection-change="onSelectionChange('teachingResume',$event)"></table-panel>
    </el-col>
  </el-row>
  <el-dialog title="教学工作信息" width="40%" :visible.sync="teachingResumeDlgVisible">
    <el-form ref="teachingResumeForm" :model="teachingResumeRecord" :rules="teachingResumeRules"
    label-position="right" label-width="90px" size="mini" status-icon>
      <el-form-item label="教师" prop="jsbh">
        <el-select v-model="teachingResumeRecord.jsbh" placeholder="选择教师" 
        :filterable="authorities.hasAllTeacher" :remote="authorities.hasAllTeacher"
        :remote-method="teacherSearching" :loading="teacherLoading">
          <template v-for="item in teacherData">
            <el-option
              :key="item.jsbh"
              :label="item.xm"
              :value="item.jsbh">
              <span style="float: left">{{ item.xm }}</span>
              <span style="float: right; font-size: 13px; margin-left:5px;">{{ item.gh }}</span>
            </el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="jxgzkssj">
        <el-date-picker style="width:100%;" v-model="teachingResumeRecord.jxgzkssj"
          type="date" value-format="timestamp" format="yyyy年MM月" placeholder="选择月">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="jxgzjssj">
        <el-date-picker style="width:100%;" v-model="teachingResumeRecord.jxgzjssj"
          type="date" value-format="timestamp" format="yyyy年MM月" placeholder="选择月">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="课程名称" prop="kcmc">
        <el-input v-model="teachingResumeRecord.kcmc"></el-input>
      </el-form-item> 
      <el-form-item label="授课对象" prop="skdx">
        <el-input v-model="teachingResumeRecord.skdx"></el-input>
      </el-form-item>
      <el-form-item label="授课学时" prop="skxs">
        <el-input v-model="teachingResumeRecord.skxs"></el-input>
      </el-form-item>
      <el-form-item label="授课学院" prop="xybh">
      <el-select v-model="teachingResumeRecord.xybh" placeholder="请选择学院" filterable>
          <template v-for="item in facultyData">
            <el-option
              :key="item.xybh"
              :label="item.xymc"
              :value="item.xybh">
              <span style="float: left">{{ item.xymc }}</span>
              <span style="float: right; font-size: 13px; margin-left:5px;">{{ item.xybhzdy }}</span>
            </el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-row>
        <el-button @click="onConfirm('teachingResume', $event)" type="primary" size="mini">确定</el-button>
        <el-button @click="onAnother('teachingResume', $event)" type="primary" size="mini">确定，再添加一个</el-button>
        <el-button @click="teachingResumeDlgVisible=false" size="mini">取消</el-button>
      </el-row>
    </el-form>
  </el-dialog> 
</div>
  
</template>
<script>
import TablePanel from "@/components/basic/TablePanel";
import {
  formModelInitFromTable,
  formRulesInitFromTable
} from "@/utils/formutils.js";
import { trueFalseRenderer,b, dateRenderer } from "@/utils/tableutils.js";

//meta info
var teachingResume = "teachingResume"; // 表格名称
var columnsAttr = "Columns"; // 列
var dataAttr = "Data"; // 数据
var selectionAttr = "Selection"; // 选中
var dlgVisibleAttr = "DlgVisible"; // 对话框
var formAttr = "Form";
var recordAttr = "Record"; // 表单模型
var rulesAttr = "Rules"; // 表单验证
var curPageAttr = "CurrentPage";// 分页-当前页
var totalNumAttr = "TotalNum";// 分页-总数
var qParamAttr = "QueryParams";// 查询参数

// 系信息-列
var teachingResumeColumns = [
  {
    prop: "jsbh",
    label: "教师编号",
    hidden: true
  },
  {
    prop:"xm",
    label:"教师姓名",
    rules: [{
      required: true,
      message:"必填",
      trigger: "blur"
    }]
  },
  {
    prop:"gh",
    label:"工号",
    rules: [{
      required: true,
      message:"必填",
      trigger: "blur"
    }]
  },
  {
    prop:"jxgzjlbh",
    label:"教学工作简历编号",
    hidden:true
  },
  {
    prop:"jxgzkssj",
    label:"开始时间",
    type:"date",
    renderer:dateRenderer,
    rules: [{
      required: true,
      message:"必填",
      trigger: "blur"
    }]
  },
  {
    prop:"jxgzjssj",
    label:"结束时间",
    type:"date",
    renderer:dateRenderer,
    rules: [{
      required: true,
      message:"必填",
      trigger: "blur"
    }]
  },
  {
    prop:"kcmc",
    label:"课程名称",
    rules: [{
      required: true,
      message:"必填",
      trigger: "blur"
    }]
  },
  {
    prop:"skdx",
    label:"授课对象",
    rules: [{
      required: true,
      message:"必填",
      trigger: "blur"
    }]
  },
  {
    prop:"skxs",
    label:"授课学时",
    rules: [{
      required: true,
      message:"必填",
      trigger: "blur"
    }]
  },
  {
    prop:"xybh",
    label:"授课学院编号",
    hidden:true,
    rules: [{
      required: true,
      message:"必填",
      trigger: "blur"
    }]
  },
  {
    prop:"xymc",
    label:"授课学院"
  }
];

export default {
  name:"teachingresume",
  components: {
    "table-panel": TablePanel
  },
  methods: {
    onSearching: function(tname, params) {
      var me = this;
      let url = "/jcJxgzjl";//  /static/data/teachingResume.json

      if(url !== "") {
        me[tname+qParamAttr] = {};
        me[tname+qParamAttr] = params;
        if (params.isAccurate) {
          me[tname+qParamAttr] = params.paramPairs;
        } else {
          me[tname+qParamAttr].keywords = params.query;
        }
        me.getRequest(url, me[tname+qParamAttr]).then(resp => {
            if(resp.status == 200) {
              me[tname+dataAttr] = resp.data;
            }
        });
      }
    },
    onCreate: function(tname) {
      // 新增
      var me = this;
      me[tname + recordAttr] = formModelInitFromTable(me[tname + columnsAttr]);
      
      me[tname + dlgVisibleAttr] = true;
    },
    onEdit: function(tname, row) {
      // 编辑
      var me = this;
      me[tname + recordAttr] = row;
      me[tname + dlgVisibleAttr] = true;
    },
    onDelete: function(tname, row) {
      // 删除
      var me = this;
      me.$confirm("确定删除这条记录吗?", "提示", {
          type: "warning"
        }).then(() => {
          me.deleteRequest(`/jcJxgzjl/${row.jxgzjlbh}`).then((resp)=>{
            if(resp.status == 200) {
              me.$message.success("删除成功!");
              me.onSearching(teachingResume,{});// 重新加载表格数据
            }else{
              me.$message.error('删除失败！');
            }
          });
        }).catch(() => {});
    },
    onExport: function(tname, params) {
      // 导出
      var me = this;
      me.$message.info("发送导出请求，返回下载路径");
    },
    onSelectionChange: function(tname, data) {
      // 选中改变
      var me = this;
      // 缓存记录，按照角色编号，查询对应的选中权限节点数组
      me[tname + selectionAttr] = data;
    },
    onConfirm: function(tname) {// 对话框-确认
      var me = this;
      // 验证信息
      var ret;
      me.$refs[tname + formAttr].validate((result, what) => {
        ret = result;
      });
      if (!ret) {
        me.$message.error("表单信息有误");
        return;
      }
      // 发送请求 /static/data/testSuc.json
      me.postRequest("/jcJxgzjl",me[tname + recordAttr]).then(resp => {
        if (resp.status == 200) {
          me.$message.success("提交成功！");
          //最后，隐藏对话框
          me[tname + dlgVisibleAttr] = false;
          me.onSearching(teachingResume, {});// 重新查一遍
        } else {
          me.$message("服务器发生错误，代码：" + resp.status);
        }
      });
    },
    onAnother:function(tname){
      //TODO 先保存记录，然后刷新表格，然后重置表单
      var me = this;
      // 验证信息
      var ret;
      me.$refs[tname + formAttr].validate((result, what) => {
        ret = result;
      });
      if (!ret) {
        me.$message.error("表单信息有误");
        return;
      }
      // 发送请求 /static/data/testSuc.json
      me.postRequest("/jcJxgzjl",me[tname + recordAttr]).then(resp => {
        if (resp.status == 200) {
          me.$message.success("提交成功！");
          //最后，隐藏对话框 
          me.onCreate(teachingResume);
          me.onSearching(teachingResume, {});// 重新查一遍
        } else {
          me.$message("服务器发生错误，代码：" + resp.status);
        }
      });
    },
    onPageChange:function(tname, page){// 分页改变
      var me = this;
      let url = "/jcJxgzjl";

      if(url !== ""){
        me[tname+curPageAttr] = page;
        me[tname+qParamAttr]["page"] = page;
        me.getRequest(url, me[tname+qParamAttr]).then(resp => {
            if(resp.status == 200) {
              me[tname+dataAttr] = resp.data.results;
              me[tname+totalNumAttr] = resp.data.total;
            }
        });
      }

    },
    teacherSearching:function(query){// 教师-远程搜索
      var me = this;
      if(me.authorities.hasAllTeacher){
        if (query !== '') {
          me.teacherLoading = true;
          let url = "/jcJsxxb";
          me.getRequest(url, { 
            xm:JSON.stringify([query]),// 这里是采用和后台的约定
            page: 1,
            size: 200000
          }).then(resp => {
              if(resp.status == 200) {
                me.teacherData = resp.data.results;
                me.teacherLoading = false;
              }
          });
        } else {
          me.teacherData = [];
        }
      }
      
    },
  },
  data() {
    return {
      authorities:{
        create:true,
        delete:true,
        edit:true,
        search:true,
        hasAllTeacher:false
      },// 权限控制
      [teachingResume + columnsAttr]: teachingResumeColumns, // 表格-列
      [teachingResume + dataAttr]: [], // 表格的数据
      [teachingResume + selectionAttr]: [], // 当前选中
      [teachingResume + dlgVisibleAttr]: false, // 对话框是否可见
      [teachingResume + recordAttr]: {}, // 表单模型
      [teachingResume + rulesAttr]: {}, // 表单验证
      facultyData:[],
      teacherData:[],
      [teachingResume+curPageAttr]:1,// 当前页
      [teachingResume+totalNumAttr]:0,// 总数
      [teachingResume+qParamAttr]:{},// 查询参数
      pageSize:20,// 每页数量
      teacherLoading:false//搜索框远程搜索辅助
    };
  },
  created: function() {
    var me = this;
    // 添加权限控制
    // me.authorities = me.$route.meta.authorities;

    // 表单模型初始化
    me[teachingResume+recordAttr] = formModelInitFromTable(me[teachingResume+columnsAttr]);
    // 表单验证初始化
    me[teachingResume+rulesAttr] = formRulesInitFromTable(me[teachingResume+columnsAttr]);

    // 加载学院信息和教师信息，如果教师信息只有一条，那么则自动填入
    me.getRequest("/jcXyxxb",{}).then(resp=>{
      if(resp.status == 200) {
        me.facultyData = resp.data;
      }
    });
    
    if(me.authorities.hasAllTeacher){
      me.getRequest("/jcJsxxb",{}).then(resp=>{
        if(resp.status == 200) {
          me.teacherData = resp.data.results;
        }
      });
    }else{
      me.getRequest("/jcJsxxb",{
        jsbh:me.$store.state.user.sslxbh
      }).then(resp=>{
        if(resp.status == 200) {
          me.teacherData = resp.data.results;
        }
      });
    }

  }
}
</script>
<style lang="scss">

.module-basic-teachingresume{
  height:100%;
  
   
}
 
</style>


